package com.sgb.link.view.widget.location;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.link.R;
import com.sgb.link.entity.trip.AirportCityEntity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Describe:
 * Author:
 * Date:    2021/7/29
 */
public class SimpleTextAdapter extends RecyclerView.Adapter<SimpleTextAdapter.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<AirportCityEntity> strings;
    private OnItemClickListener itemClickListener;

    public Context getContext() {
        return context;
    }

    public SimpleTextAdapter(Context context, List<AirportCityEntity> strings) {
        this.context = context;
        this.strings = strings;
    }

    public OnItemClickListener getItemClickListener() {
        return itemClickListener;
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_text_hot_city, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull SimpleTextAdapter.ViewHolder holder, int position) {
        holder.city.setText(strings.get(position).getCityName());
        holder.city.setOnClickListener(v -> {
            if (itemClickListener != null)
                itemClickListener.onItemClick(position, strings.get(position));
        });
    }


    @Override
    public int getItemCount() {
        return strings.size();
    }

    @Override
    public void onClick(View v) {

    }


    /**
     * 正常内容的viewholder
     */
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView city;

        ViewHolder(View itemView) {
            super(itemView);
            city = itemView.findViewById(R.id.name);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, AirportCityEntity entity);
    }
}
